package com.struts.web.manage;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.lg.system.CLGAction;
import org.apache.struts.action.ActionForward;

import com.lg.system.CSqlMap;
import com.base.Constants;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.po.system.TSysAdmin;
import com.ibatis.po.user.TUser;
import com.ibatis.po.user.TUserInfo;
import com.struts.web.manage.LoginForm;
import com.ibatis.po.corpor.TCorporation;

public class LoginAction extends CLGAction {
	
	/**
	 * 用户登录�û���¼
	 * @param actionMapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
    public static ActionForward userLogin(ActionMapping actionMapping,
            ActionForm actionForm,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
    	try{
    	LoginForm loginForm = (LoginForm)actionForm;
    	TUserInfo user = new TUserInfo();
    	user.setLoginName(loginForm.getLoginName());
    	user.setPassword(loginForm.getLoginPassword());
    	SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
    	user=(TUserInfo)sqlmap.queryForObject("T_USER_INFO.selectByNameAndPass",user);
    	
    	if(user!=null){
    		sqlmap.update("T_USER_INFO.updateLoginCount",user);
    		request.getSession().setAttribute("TUser", user);
    	}else{
    		return actionMapping.findForward("validateLogin");
    	}
    	}catch(Exception e){
    		logger.info("��ִ��LoginAction���е�userLogin����ʱ��?\n");
    		e.printStackTrace();
    	}
    	return actionMapping.findForward("userLoginSuccess");
    }
    
    /**
	 * 用户退出�û��˳�
	 * @param actionMapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
    public static ActionForward userLogout(ActionMapping actionMapping,
            ActionForm actionForm,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
    	try{
    		request.getSession().removeAttribute("TUser");
    	}catch(Exception e){
    		logger.info("��ִ��LoginAction���е�userLogout����ʱ��?\n");
    		e.printStackTrace();
    	}
    	return actionMapping.findForward("userLogoutSuccess");
    }
    
    /**
	 * ��企业登录��¼
	 * @param actionMapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
    public  ActionForward corporLogin(ActionMapping actionMapping,
            ActionForm actionForm,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
    	ActionMessages msgs = new ActionMessages();
    	try{
    		String rand = (String)request.getSession().getAttribute("rand");

    		
        	LoginForm loginForm = (LoginForm)actionForm;
        	TCorporation corpor = new TCorporation();
        	System.out.println("rand:"+rand);
        	if(loginForm.getLoginName()==null || loginForm.getLoginName()=="" || rand==""){
        		return actionMapping.getInputForward();
        	}
        	corpor.setLoginName(loginForm.getLoginName());
        	corpor.setPassWord(loginForm.getLoginPassword());
        	SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
        	
        	if(!loginForm.getInputnum().equals(rand)){
        		msgs.add("commonMsgs",new ActionMessage(Constants.VAL_NUM_ERR));
				saveErrors(request,msgs);
        		return actionMapping.findForward("fail");
        	}
        	
        	corpor=(TCorporation)sqlmap.queryForObject("T_CORPORATION.selectForLogin",corpor);
        	if(corpor!=null){
        		logger.info("corpor_login:"+loginForm.getLoginName()+"..pass:"+loginForm.getLoginPassword());
        		request.getSession().setAttribute("Corporation", corpor);
        	}else{
        		return actionMapping.findForward("validateLogin");
        	}
        	}catch(Exception e){
        		logger.info("��ִ��LoginAction���е�corporLogin����ʱ��?\n");
        		e.printStackTrace();
        	}
        	return actionMapping.findForward("corporLoginSuccess");
    }
    
    /**
	 * ��企业退出�˳�
	 * @param actionMapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
    public static ActionForward corporLogout(ActionMapping actionMapping,
            ActionForm actionForm,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
    	try{
    		request.getSession().removeAttribute("Corporation");
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	return actionMapping.findForward("corporLogoutSuccess");
    }
    
    /**
	 * �ֳ�չλԤ����¼
	 * @param actionMapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
    public static ActionForward onsiteLogin(ActionMapping actionMapping,
            ActionForm actionForm,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
    	try{
        	LoginForm loginForm = new LoginForm();
        	TUser user = new TUser();
        	user.setLoginname(loginForm.getLoginName());
        	user.setPassword(loginForm.getLoginPassword());
        	SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
        	user=(TUser)sqlmap.queryForObject("T_USER.selectForLogin",user);
        	if(user!=null){
        		request.getSession().setAttribute("TUser", user);
        	}
        	}catch(Exception e){
        		e.printStackTrace();
        	}
        	return actionMapping.findForward("loginSuccess");
    }
    
    
    /**
	 * ��系统管理吊登录��¼
	 * @param actionMapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
    public  ActionForward adminLogin(ActionMapping actionMapping,
            ActionForm actionForm,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
    	ActionMessages msgs = new ActionMessages();
    	List adminPopeList = null;
    	int count = 0;
    	try{
    		//验证码
    		String rand = (String)request.getSession().getAttribute("randAdmin");
        	LoginForm loginForm = (LoginForm)actionForm;
        	if(!loginForm.getInputnum().equals(rand)){
        		msgs.add("commonMsgs",new ActionMessage(Constants.VAL_NUM_ERR));
				saveErrors(request,msgs);
        		return actionMapping.findForward("fail");
        	}
        	TSysAdmin adminPo = new TSysAdmin();
        	adminPo.setLoginName(loginForm.getLoginName());
        	adminPo.setPassword(loginForm.getLoginPassword());
        	SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
        	
        	
        	
        	adminPo = (TSysAdmin)sqlmap.queryForObject("T_SYS_ADMIN.selectByloginAndPass",adminPo);
        	System.out.println("44444444444444444444444:"+adminPo.getName());
        	if(adminPo!=null){
        		//设置权限
        	    String userId = adminPo.getId();
        	    //查询用户权限是否有效
        	    count = (Integer)sqlmap.queryForObject("T_POPEDOM.selectIsValid",userId);
        	    if(count<1){
        	    	msgs.add("commonMsgs",new ActionMessage(Constants.NOT_VALID));
    				saveErrors(request,msgs);
            		return actionMapping.findForward("fail");
        	    }
        	    
        		adminPopeList = sqlmap.queryForList("T_POPECODE.selectById",userId);
        		System.out.println("test:"+adminPopeList.size());
        		request.getSession().setAttribute("adminPopeList", adminPopeList);
        		adminPo.setPassword("");
        		request.getSession().setAttribute("admin", adminPo);
        	}else{
        		msgs.add("commonMsgs",new ActionMessage(Constants.ADMIN_LOGINERROR_KEY));
				saveErrors(request,msgs);
        		return actionMapping.findForward("fail");
        	}
        	}catch(Exception e){
        		
        		e.printStackTrace();
        	}
        	return actionMapping.findForward("adminLoginSuccess");
    }
    
    /**
	 * ��系统管理员退出�˳�
	 * @param actionMapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
    public static ActionForward adminLogout(ActionMapping actionMapping,
            ActionForm actionForm,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
    	try{
    		request.getSession().removeAttribute("admin");
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	return actionMapping.findForward("adminLogoutSuccess");
    }
}
